Ext.define('NF.view.farm.ExpenseDetails', {
    extend:'Ext.grid.Panel',
    alias:'widget.expenseDetails',
    frame:true,
    activatedAtleastOnce:false,
    autoScroll:true,
    selType: 'rowmodel',

    initComponent:function(){
       var self=this;
        this.plugins = [
            Ext.create('Ext.grid.plugin.RowEditing', {
                clicksToEdit: 1
            })
        ],
        this.columns = [
            {header: 'ID',  dataIndex: 'id'},
            {header: 'Date Of Payment', dataIndex: 'dateOfPayment', flex:1,
                editor: {xtype:'datefield',format: 'F j, Y',allowBlank:false},
                renderer: function(dateOfLactation){
                    return Ext.util.Format.date(dateOfLactation,'F j, Y')
                }
            },
            {header: 'Type', dataIndex: 'expenseType', flex:1},
            {header: 'Cost', dataIndex: 'cost', flex:1}
        ],
        this.store = Ext.create('Ext.data.Store',{
            model:'NF.model.ExpenseDetails',
            autoLoad:false,
            pageSize:5,
            autoSync:false,
            proxy:{
               type:'ajax',
                url:'expense/getLivestockExpenses.json',
                reader:{
                    type:'json',
                    root:'expenseDetails',
                    totalProperty:'total'
                }
            },
            listeners:{
                update:self.handleUpdate
            }
        });
        this.dockedItems = [
            {
                xtype: 'pagingtoolbar',
                store: self.store,
                dock: 'top',
                displayInfo: true,
                items:[
                    {
                        xtype:'button',
                        iconCls: 'icon-add',
                        text: 'Add Expense',
                        listeners:{
                            click:self.addNewRecord,
                            context:self.context,
                            selfer:self
                        }
                    }
                ]
            }
        ],//dockedItems
        this.callParent(arguments);
        this.setTitle("Expense Details");
        self.activatedAtleastOnce=false;
    },
    handleUpdate:function(thisStore, updatedRecord, operation, options){
        Ext.Ajax.request({
            url: 'livestock/updateLactationDetails.json',
            params: updatedRecord.data,
            success: function(response){
                var text = response.responseText;
                thisStore.load();
            }
        });
    },
     prepareGrid:function(){
        var self=this;
        var expensesStore = self.getStore();
        expensesStore.getProxy().extraParams.id = self.context.id;
        expensesStore.load();
    },
    addNewRecord:function(thisButton,eventObject,contextObject){
        var self=this;
    }
});